<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>人员随机分组工具</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
            color: #333;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            backdrop-filter: blur(10px);
        }

        .header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            text-align: center;
            padding: 30px 20px;
        }

        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            font-weight: 300;
        }

        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        .content {
            padding: 40px 30px;
        }

        .input-section {
            margin-bottom: 40px;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #555;
            font-size: 1.1rem;
        }

        .form-group textarea {
            width: 100%;
            min-height: 120px;
            padding: 15px;
            border: 2px solid #e1e5e9;
            border-radius: 12px;
            font-size: 1rem;
            font-family: inherit;
            resize: vertical;
            transition: all 0.3s ease;
            background: #f8f9fa;
        }

        .form-group textarea:focus {
            outline: none;
            border-color: #4facfe;
            background: white;
            box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
        }

        .form-group input {
            width: 100%;
            padding: 15px;
            border: 2px solid #e1e5e9;
            border-radius: 12px;
            font-size: 1rem;
            font-family: inherit;
            transition: all 0.3s ease;
            background: #f8f9fa;
        }

        .form-group input:focus {
            outline: none;
            border-color: #4facfe;
            background: white;
            box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
        }

        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 15px 40px;
            font-size: 1.1rem;
            font-weight: 600;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
            width: 100%;
            margin-top: 20px;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 12px 25px rgba(102, 126, 234, 0.4);
        }

        .btn:active {
            transform: translateY(0);
        }

        .btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .result-section {
            display: none;
            margin-top: 40px;
        }

        .result-section.show {
            display: block;
            animation: fadeInUp 0.6s ease;
        }

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .result-title {
            text-align: center;
            font-size: 1.8rem;
            color: #333;
            margin-bottom: 30px;
            font-weight: 600;
        }

        /* 弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            animation: fadeIn 0.3s ease;
        }

        .modal-content {
            background-color: #fff;
            margin: 5% auto;
            padding: 0;
            border-radius: 12px;
            width: 90%;
            max-width: 800px;
            max-height: 80vh;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            animation: slideIn 0.3s ease;
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20px 30px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .modal-header h2 {
            margin: 0;
            font-size: 1.5rem;
        }

        .close {
            color: white;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            transition: opacity 0.3s ease;
        }

        .close:hover {
            opacity: 0.7;
        }

        .modal-body {
            padding: 30px;
            max-height: 60vh;
            overflow-y: auto;
        }

        .modal-footer {
            display: flex;
            justify-content: flex-end;
            gap: 15px;
            padding: 20px 30px;
            background-color: #f8f9fa;
            border-top: 1px solid #e1e5e9;
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        @keyframes slideIn {
            from { 
                opacity: 0;
                transform: translateY(-50px);
            }
            to { 
                opacity: 1;
                transform: translateY(0);
            }
        }

        .groups-container {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 30px;
        }

        .group {
            background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 8px 20px rgba(252, 182, 159, 0.3);
            border: 2px solid rgba(255, 255, 255, 0.5);
            flex: 1;
            min-width: 200px;
        }

        .group:nth-child(even) {
            background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
            box-shadow: 0 8px 20px rgba(168, 237, 234, 0.3);
        }

        .group:nth-child(3n) {
            background: linear-gradient(135deg, #d299c2 0%, #fef9d7 100%);
            box-shadow: 0 8px 20px rgba(210, 153, 194, 0.3);
        }

        .group-title {
            font-size: 1.3rem;
            font-weight: 700;
            color: #333;
            margin-bottom: 15px;
            text-align: center;
            padding: 10px;
            background: rgba(255, 255, 255, 0.7);
            border-radius: 10px;
        }

        .group-members {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }

        .member {
            background: rgba(255, 255, 255, 0.9);
            padding: 8px 15px;
            border-radius: 20px;
            font-weight: 500;
            color: #555;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }

        .member:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }

        .reset-btn {
            background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
            color: white;
            border: none;
            padding: 12px 30px;
            font-size: 1rem;
            font-weight: 600;
            border-radius: 25px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 6px 15px rgba(255, 107, 107, 0.3);
            display: block;
            margin: 0 auto;
        }

        .reset-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(255, 107, 107, 0.4);
        }

        .tips {
            background: rgba(79, 172, 254, 0.1);
            border: 1px solid rgba(79, 172, 254, 0.3);
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 30px;
            color: #555;
        }

        .tips h3 {
            color: #4facfe;
            margin-bottom: 8px;
            font-size: 1.1rem;
        }

        .tips ul {
            margin-left: 20px;
        }

        .tips li {
            margin-bottom: 5px;
        }

        .checkbox-container {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 12px;
            margin-bottom: 20px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 12px;
            border: 2px solid #e1e5e9;
        }

        .checkbox-item {
            display: flex;
            align-items: center;
            padding: 10px 15px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .checkbox-item:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        .checkbox-item input[type="checkbox"] {
            width: 18px;
            height: 18px;
            margin-right: 12px;
            cursor: pointer;
            accent-color: #4facfe;
        }

        .checkbox-item label {
            margin: 0;
            cursor: pointer;
            font-weight: 500;
            color: #555;
            flex: 1;
        }

        .checkbox-actions {
            display: flex;
            gap: 10px;
            justify-content: center;
            margin-top: 15px;
        }

        .select-all-btn, .select-none-btn {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            border: none;
            padding: 8px 20px;
            font-size: 0.9rem;
            font-weight: 600;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 10px rgba(79, 172, 254, 0.3);
        }

        .select-none-btn {
            background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
            box-shadow: 0 4px 10px rgba(255, 107, 107, 0.3);
        }

        .select-all-btn:hover, .select-none-btn:hover {
            transform: translateY(-1px);
            box-shadow: 0 6px 15px rgba(79, 172, 254, 0.4);
        }

        .select-none-btn:hover {
            box-shadow: 0 6px 15px rgba(255, 107, 107, 0.4);
        }

        /* 移动端适配 */
        @media (max-width: 768px) {
            body {
                padding: 10px;
            }

            .header h1 {
                font-size: 2rem;
            }

            .content {
                padding: 20px 15px;
            }

            .form-group textarea {
                min-height: 100px;
            }

            .groups-container {
                gap: 15px;
            }

            .modal-content {
                width: 95%;
                margin: 10% auto;
                max-height: 85vh;
            }

            .modal-header {
                padding: 15px 20px;
            }

            .modal-header h2 {
                font-size: 1.2rem;
            }

            .modal-body {
                padding: 20px;
                max-height: 50vh;
            }

            .modal-footer {
                padding: 15px 20px;
                flex-direction: column;
                gap: 10px;
            }

            .modal-footer .btn,
            .modal-footer .reset-btn {
                width: 100%;
                margin: 0;
            }

            .group {
                padding: 15px;
            }

            .group-title {
                font-size: 1.1rem;
            }

            .member {
                padding: 6px 12px;
                font-size: 0.9rem;
            }

            .checkbox-container {
                grid-template-columns: 1fr;
                padding: 15px;
            }

            .checkbox-item {
                padding: 8px 12px;
            }
        }

        @media (max-width: 480px) {
            .header h1 {
                font-size: 1.8rem;
            }

            .header p {
                font-size: 1rem;
            }

            .content {
                padding: 15px 10px;
            }

            .group-members {
                gap: 8px;
            }

            .member {
                padding: 5px 10px;
                font-size: 0.85rem;
            }

            .checkbox-container {
                padding: 10px;
                grid-template-columns: repeat(2, 1fr);
            }

            .checkbox-item {
                padding: 6px 10px;
            }

            .select-all-btn, .select-none-btn {
                padding: 6px 15px;
                font-size: 0.8rem;
            }

            .groups-container {
                flex-direction: column;
            }

            .group {
                min-width: auto;
                flex: none;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🎯 人员随机分组</h1>
            <p>快速、公平、随机的人员分组工具</p>
        </div>

        <div class="content">


            <div class="input-section">
                <div class="form-group">
                    <label>👥 选择参与分组的人员</label>
                    <div class="checkbox-container">
                        <div class="checkbox-item">
                            <input type="checkbox" id="person1" value="李伟" checked>
                            <label for="person1">李伟</label>
                        </div>
                        <div class="checkbox-item">
                            <input type="checkbox" id="person2" value="周天凡" checked>
                            <label for="person2">周天凡</label>
                        </div>
                        <div class="checkbox-item">
                            <input type="checkbox" id="person3" value="夏培轩" checked>
                            <label for="person3">夏培轩</label>
                        </div>
                        <div class="checkbox-item">
                            <input type="checkbox" id="person4" value="孟宇" checked>
                            <label for="person4">孟宇</label>
                        </div>
                        <div class="checkbox-item">
                            <input type="checkbox" id="person5" value="刘诗龙" checked>
                            <label for="person5">刘诗龙</label>
                        </div>
                        <div class="checkbox-item">
                            <input type="checkbox" id="person6" value="段桦" checked>
                            <label for="person6">段桦</label>
                        </div>
                        <div class="checkbox-item">
                            <input type="checkbox" id="person7" value="施朝辉" checked>
                            <label for="person7">施朝辉</label>
                        </div>
                        <div class="checkbox-item">
                            <input type="checkbox" id="person8" value="胡淫涛" checked>
                            <label for="person8">胡淫涛</label>
                        </div>
                        <div class="checkbox-item">
                            <input type="checkbox" id="person9" value="秦训军" checked>
                            <label for="person9">秦训军</label>
                        </div>
                    </div>
                    <div class="checkbox-actions">
                        <button type="button" class="select-all-btn" onclick="selectAll()">全选</button>
                        <button type="button" class="select-none-btn" onclick="selectNone()">全不选</button>
                    </div>
                </div>

                <div class="form-group">
                    <label for="groupSize">🔢 每组人数</label>
                    <input type="number" id="groupSize" placeholder="请输入每组人数" min="1" value="3">
                </div>

                <button class="btn" onclick="startGrouping()">🎲 开始分组</button>
            </div>

            <!-- 分组结果弹窗 -->
            <div class="modal" id="resultModal">
                <div class="modal-content">
                    <div class="modal-header">
                        <h2>📋 分组结果</h2>
                        <span class="close" onclick="closeModal()">&times;</span>
                    </div>
                    <div class="modal-body">
                        <div class="groups-container" id="groupsContainer"></div>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" onclick="closeModal()">关闭</button>
                        <button class="reset-btn" onclick="resetGrouping()">🔄 重新分组</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        function startGrouping() {
            const groupSize = parseInt(document.getElementById('groupSize').value);

            // 获取选中的人员
            const checkboxes = document.querySelectorAll('.checkbox-item input[type="checkbox"]:checked');
            const names = Array.from(checkboxes).map(checkbox => checkbox.value);

            // 验证输入
            if (names.length === 0) {
                alert('请至少选择一个人参与分组！');
                return;
            }

            if (!groupSize || groupSize < 1) {
                alert('请输入有效的每组人数！');
                return;
            }

            if (groupSize > names.length) {
                alert('每组人数不能大于选中的人数！');
                return;
            }

            // 随机分组
            const groups = randomGrouping(names, groupSize);
            displayGroups(groups);

            // 显示弹窗
            document.getElementById('resultModal').style.display = 'block';
        }

        function randomGrouping(names, groupSize) {
            // 复制数组并随机打乱
            const shuffledNames = [...names].sort(() => Math.random() - 0.5);
            
            const groups = [];
            let currentGroup = [];
            
            for (let i = 0; i < shuffledNames.length; i++) {
                currentGroup.push(shuffledNames[i]);
                
                // 当当前组达到指定人数或者是最后一个人时，完成这一组
                if (currentGroup.length === groupSize || i === shuffledNames.length - 1) {
                    groups.push([...currentGroup]);
                    currentGroup = [];
                }
            }
            
            return groups;
        }

        function displayGroups(groups) {
            const container = document.getElementById('groupsContainer');
            container.innerHTML = '';

            groups.forEach((group, index) => {
                const groupDiv = document.createElement('div');
                groupDiv.className = 'group';
                
                const titleDiv = document.createElement('div');
                titleDiv.className = 'group-title';
                titleDiv.textContent = `第 ${index + 1} 组 (${group.length}人)`;
                
                const membersDiv = document.createElement('div');
                membersDiv.className = 'group-members';
                
                group.forEach(member => {
                    const memberDiv = document.createElement('div');
                    memberDiv.className = 'member';
                    memberDiv.textContent = member;
                    membersDiv.appendChild(memberDiv);
                });
                
                groupDiv.appendChild(titleDiv);
                groupDiv.appendChild(membersDiv);
                container.appendChild(groupDiv);
            });
        }

        function closeModal() {
            document.getElementById('resultModal').style.display = 'none';
        }

        function resetGrouping() {
            closeModal();
            document.getElementById('groupsContainer').innerHTML = '';
        }

        // 点击弹窗外部关闭弹窗
        window.onclick = function(event) {
            const modal = document.getElementById('resultModal');
            if (event.target === modal) {
                closeModal();
            }
        }

        // 添加回车键支持
        document.getElementById('groupSize').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                startGrouping();
            }
        });

        // 添加输入验证
        document.getElementById('groupSize').addEventListener('input', function(e) {
            const value = parseInt(e.target.value);
            if (value < 1) {
                e.target.value = 1;
            }
        });

        // 全选功能
        function selectAll() {
            const checkboxes = document.querySelectorAll('.checkbox-item input[type="checkbox"]');
            checkboxes.forEach(checkbox => {
                checkbox.checked = true;
            });
        }

        // 全不选功能
        function selectNone() {
            const checkboxes = document.querySelectorAll('.checkbox-item input[type="checkbox"]');
            checkboxes.forEach(checkbox => {
                checkbox.checked = false;
            });
        }
    </script>
</body>
</html>